﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HM
{
    [Description("直接调拨单获取51JOB物流信息")]
    public class test51Job : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.PreparePropertysEventArgs e)
        {
            e.FieldKeys.Add("FID");
            base.OnPreparePropertys(e);
        }

        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            base.EndOperationTransaction(e);
            string FID = "", F_RXWU_TEXT5 = "", url = "", Result="", code="", courier_code="", create="", createurl="",GetResult="",Geturl="",
                latest_event="", DeleteURL="",ID="", FENTRYID="", F_RXWU_Text15="", tracking_number="", delivery_status="";
            string latest_checkpoint_time="";
            url = "https://api.51tracking.com/v3/trackings/detect";
            createurl = "https://api.51Tracking.com/v4/trackings/create";
            Geturl = "https://api.51tracking.com/v4/trackings/get";
            DeleteURL = "https://api.51Tracking.com/v4/trackings/delete?id=";
            foreach (DynamicObject item in e.DataEntitys)
            {
                FID = Convert.ToString(item["ID"]);
                string SQL = "/*dialect*/" + string.Format(@" SELECT F_RXWU_TEXT6 AS F_RXWU_TEXT5,FENTRYID,F_RXWU_Text15 FROM T_STK_STKTRANSFERINENTRY WHERE F_RXWU_TEXT6<>'' AND FID='{0}'", FID);
                DynamicObjectCollection DynamData = DBUtils.ExecuteDynamicObject(this.Context, SQL);
                if (DynamData.Count > 0)
                {
                    for (int i = 0; i < DynamData.Count; i++)
                    {
                        F_RXWU_TEXT5 = Convert.ToString(DynamData[i]["F_RXWU_TEXT5"]);
                        FENTRYID = Convert.ToString(DynamData[i]["FENTRYID"]);
                        F_RXWU_Text15= Convert.ToString(DynamData[i]["F_RXWU_Text15"]);
                        if(!F_RXWU_Text15.Equals("")&& !F_RXWU_Text15.Equals(" "))
                        {
                            //删除单号创建信息
                            DeleteNumber.Delete(F_RXWU_Text15, DeleteURL);
                        }
                        JObject Obj = new JObject();
                        //获取物料商编码
                        Obj.Add("tracking_number", F_RXWU_TEXT5);
                        Result = PostMan.Post(url, Obj.ToString());
                        JObject jobject = JObject.Parse(Result);
                        code = Convert.ToString(jobject["code"]);
                        if(code.Equals("200"))
                        {
                            //获取物料商编码
                            courier_code = Convert.ToString(jobject["data"][0]["courier_code"]);
                            //创建物流商
                            JObject ObjCreate = new JObject();
                            ObjCreate.Add("tracking_number", F_RXWU_TEXT5);
                            ObjCreate.Add("courier_code", courier_code);
                            create = PostMan.Post(createurl, ObjCreate.ToString());
                            JObject jobject0 = JObject.Parse(create);
                            code = Convert.ToString(jobject0["meta"]["code"]);
                            if(code.Equals("200"))
                            {
                                ID= Convert.ToString(jobject0["data"]["id"]);
                                string SQL1 = "/*dialect*/" + string.Format(@"update T_STK_STKTRANSFERINENTRY set F_RXWU_Text15='{1}' where FENTRYID='{0}'", FENTRYID, ID);
                                DBUtils.Execute(this.Context, SQL1);
                                //获取物流轨迹
                                Dictionary<string, string> dictionary = new Dictionary<string, string>();
                                dictionary.Add("tracking_numbers", F_RXWU_TEXT5);
                                GetResult = Gettrackings.Get(Geturl, dictionary);
                                JObject jobject1 = JObject.Parse(GetResult);
                                code = Convert.ToString(jobject1["meta"]["code"]);
                                if(code.Equals("200"))
                                {
                                    if(jobject1["data"]["success"].Count()>0)
                                    {
                                        for(int k=0;k< jobject1["data"]["success"].Count();k++)
                                        {
                                            tracking_number = Convert.ToString(jobject1["data"]["success"][k]["tracking_number"]);
                                            if(tracking_number.Equals(F_RXWU_TEXT5))
                                            {
                                                latest_event = Convert.ToString(jobject1["data"]["success"][k]["latest_event"]);
                                                delivery_status = Convert.ToString(jobject1["data"]["success"][k]["delivery_status"]);
                                                latest_checkpoint_time = Convert.ToString(jobject1["data"]["success"][k]["latest_checkpoint_time"]);
                                                latest_event = BaiduAPI.GET(latest_event);
                                                JObject jobject2 = JObject.Parse(latest_event);
                                                latest_event =Convert.ToString(jobject2["trans_result"][0]["dst"]);
                                                delivery_status = BaiduAPI.GET(delivery_status);
                                                JObject jobject3 = JObject.Parse(delivery_status);
                                                delivery_status = Convert.ToString(jobject3["trans_result"][0]["dst"]);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
